import 'package:bazi/model/jieqi_model.dart';
import 'package:bazi/model/paipan_model.dart';
import 'package:bazi/model/sizhu_model.dart';
import 'package:bazi/util/my_color.dart';
import 'package:bazi/util/paipan.dart';
import 'package:flutter/material.dart';

import 'calendar_widget.dart';

class HeHunPage extends StatefulWidget {
  const HeHunPage({Key? key}) : super(key: key);

  @override
  _HeHunState createState() => _HeHunState();
}

class _HeHunState extends State<HeHunPage> with AutomaticKeepAliveClientMixin{
  bool _isFemale = true;
  TextEditingController _meController = new TextEditingController();
  TextEditingController _otherController = new TextEditingController();

  ///世纪
  var _meCentury = 0;
  var _otherCentury = 0;
  ///年
  var _meYear = 0;
  var _otherYear = 0;
  ///月
  var _meMonth = 0;
  var _otherMonth = 0;
  ///日
  var _meDay = 0;
  var _otherDay = 0;
  ///时
  var _meHour = 0;
  var _otherHour = 0;
  String _meDate ='';
  String _otherDate ='';
  String _meLunraDate='';
  String _otherLunraDate='';
  bool _meIsLunar = false;
  bool _otherIsLunar = false;

  late JieqiModel _meJieqiModel;
  late JieqiModel _otherJieqiModel;
  var _paipanUtil = PaipanUtil.instance();

  ///四柱八字

  var _meSizhuModel = SizhuModel();
  var _otherSizhuModel = SizhuModel();
  var _SXMatching = '';
  var _riZhuMatching = '';
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    DateTime dateTime = DateTime.now();
    _meDate = "公历:${dateTime.year}年${dateTime.month}月${dateTime.day}日 00点";
    _otherDate = "公历:${dateTime.year}年${dateTime.month}月${dateTime.day}日 00点";
    _meCentury = int.parse(dateTime.year.toString().substring(0, 2));
    _otherCentury = int.parse(dateTime.year.toString().substring(0, 2));
    _meYear = int.parse(dateTime.year.toString().substring(2, 4));
    _otherYear = int.parse(dateTime.year.toString().substring(2, 4));
    _meMonth = dateTime.month;
    _otherMonth = dateTime.month;
    _otherDay = dateTime.day;

  }
  void submit(){
    ///节气
    _meJieqiModel = _paipanUtil.getJieqi(_meCentury, _meYear);
    _otherJieqiModel = _paipanUtil.getJieqi(_otherCentury, _otherYear);

    ///四柱
    _meSizhuModel = _paipanUtil.getSizhu(_meCentury,_meYear,_meMonth,_meDay,_meHour,_meJieqiModel);
    _otherSizhuModel = _paipanUtil.getSizhu(_otherCentury,_otherYear,_otherMonth,_otherDay,_otherHour,_otherJieqiModel);

    var meYearZ = _meSizhuModel.yearZ;
    var otherYearZ = _otherSizhuModel.yearZ;
    if(_paipanUtil.liuChong.contains(meYearZ+otherYearZ)||_paipanUtil.liuChong.contains(otherYearZ+meYearZ)){
      _SXMatching = "$meYearZ与$otherYearZ相冲，匹配指数-5";
    }else if(_paipanUtil.liuHai.contains(meYearZ+otherYearZ)||_paipanUtil.liuHai.contains(otherYearZ+meYearZ)){
      _SXMatching = "$meYearZ与$otherYearZ相害，匹配指数-5";
    }else if(_paipanUtil.liuHe.contains(meYearZ+otherYearZ)||_paipanUtil.liuHe.contains(otherYearZ+meYearZ)){
      _SXMatching = "$meYearZ与$otherYearZ相合，匹配指数+5";
    }else{
      _SXMatching = "$meYearZ与$otherYearZ无冲无合，匹配指数0";
    }
    int ganIndex = _paipanUtil.gans.indexOf(_meSizhuModel.dayG);
    //日柱是否相克
    var ganKe = false;
    var zhiKe = false;
    var ganHe = false;
    var zhiHe = false;
    if(_paipanUtil.gans[(ganIndex+4)%10] == _otherSizhuModel.dayG||_paipanUtil.gans[(ganIndex+6)%10] == _otherSizhuModel.dayG){
      ganKe = true;
    }
    if(_paipanUtil.liuChong.contains(_meSizhuModel.dayZ+_otherSizhuModel.dayZ)
        ||_paipanUtil.liuChong.contains(_otherSizhuModel.dayZ+_meSizhuModel.dayZ)){
      zhiKe = true;
    }
    if(_paipanUtil.wuHe.contains(_meSizhuModel.dayZ+_otherSizhuModel.dayZ)
        ||_paipanUtil.wuHe.contains(_otherSizhuModel.dayZ+_meSizhuModel.dayZ)){
      ganHe = true;
    }
    if(_paipanUtil.liuHe.contains(_meSizhuModel.dayZ+_otherSizhuModel.dayZ)
        ||_paipanUtil.liuHe.contains(_otherSizhuModel.dayZ+_meSizhuModel.dayZ)){
      zhiHe = true;
    }
    if(ganKe&&zhiKe){
      _riZhuMatching = "日柱天克地冲，匹配指数-10";
    }else if(ganKe&&!zhiKe){
      _riZhuMatching = "日柱天干相克，匹配指数-5";
    }else if(!ganKe&&zhiKe){
      _riZhuMatching = "日柱地支相冲，匹配指数-5";
    }else{
      if(ganHe&&zhiHe){
        _riZhuMatching = "日柱天地鸳鸯合，匹配指数+10";
      }else if(ganHe&&!zhiHe){
        _riZhuMatching = "日柱天干五合，匹配指数+5";
      }else if(!ganHe&&zhiHe){
        _riZhuMatching = "日柱地支六合，匹配指数+5";
      }else{
        _riZhuMatching = "日柱无冲无合，匹配指数+0";
      }
    }

    setState(() {

    });

  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColor.hehunBg,
      body: ListView(
        padding: EdgeInsets.zero,
        children: [
          Image.asset("assets/hehun_1.png",width: double.infinity,),
          //填写资料
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Stack(
              children: [
                Image.asset("assets/hehun_2.png",width: double.infinity,height: 260,fit:BoxFit.fill,),
                Container(
                  height: 210,
                  width: double.infinity,
                  margin: EdgeInsets.only(top: 30),
                  child:Column(
                    children: [
                    Expanded(
                      child: Container(
                        margin: EdgeInsets.symmetric(horizontal: 20,vertical: 10),
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        color:Color(0xfffbf7eb),
                        width: double.infinity,
                        child: Column(children: [
                          Row(
                            children: [
                              SizedBox(width: 10,),
                              Text("测算者:",style: TextStyle(color: MyColor.primaryColor,fontSize: 15),),
                              SizedBox(width: 10,),
                              Expanded(child: TextField(
                                controller: _meController,
                                decoration: InputDecoration(border: InputBorder.none),
                              )),
                             GestureDetector(child: Image.asset(_isFemale?"assets/female.png":"assets/male.png",height: 26,),
                               onTap: (){
                                _isFemale = !_isFemale;
                                setState(() {

                                });
                               },
                             )
                            ],
                          ),
                          Divider(thickness: 2,color: MyColor.primaryColor,height: 0,),
                          SizedBox(height: 10,),
                          Row(
                            children: [
                              SizedBox(width: 10,),
                              Text("生辰:",style: TextStyle(color: MyColor.primaryColor,fontSize: 15),),
                              SizedBox(width: 10,),
                              Expanded(child: GestureDetector(
                                onTap: (){
                                  _dateSelected(true);
                                },
                                child: Text(_meIsLunar?_meLunraDate:_meDate,style: TextStyle(fontSize: 15),
                                ),
                              )),

                            ],
                          ),
                        ],),
                      ),
                    ),
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 20,vertical: 10),
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          color:Color(0xfffbf7eb),
                          width: double.infinity,
                          child: Column(children: [
                            Row(
                              children: [
                                SizedBox(width: 10,),
                                Text("对方姓氏:",style: TextStyle(color: MyColor.primaryColor,fontSize: 15),),
                                SizedBox(width: 10,),
                                Expanded(child: TextField(
                                  controller: _otherController,
                                  decoration: InputDecoration(border: InputBorder.none),
                                )),
                              ],
                            ),
                            Divider(thickness: 2,color: MyColor.primaryColor,height: 0,),
                            SizedBox(height: 10,),
                            Row(
                              children: [
                                SizedBox(width: 10,),
                                Text("对方生辰:",style: TextStyle(color: MyColor.primaryColor,fontSize: 15),),
                                SizedBox(width: 10,),
                                Expanded(child: GestureDetector(
                                  onTap: (){
                                    _dateSelected(false);
                                  },
                                  child: Text(_otherIsLunar?_otherLunraDate:_otherDate,style: TextStyle(fontSize: 15),
                                  ),
                                )),

                              ],
                            ),
                          ],),
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
          //提交
          GestureDetector(
            onTap: submit,
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 20),
                child: Stack(
                  alignment: Alignment.center,
              children: [
                Image.asset("assets/hehun_btn.png",height: 50,),
                Text("马上查看",style: TextStyle(color: MyColor.primaryColor,fontWeight: FontWeight.w700,fontSize: 16,letterSpacing: 5),)
              ],
            )),
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 10,vertical: 20),
            child: Column(children: [
              Image.asset("assets/hehun_bg2.png"),
              SizedBox(height: 20,),
              Image.asset("assets/hehun_bg3.png"),
              SizedBox(height: 20,),
              Image.asset("assets/hehun_bg4.png"),
              SizedBox(height: 20,)
            ],),
          ),
          Text("生肖匹配指数："+_SXMatching)
        ],
      ),
    );
  }
  ///日期
  void _dateSelected(bool isMe){
    showDialog(
        barrierDismissible: false,
        context: context,
        builder: (context) {
          return isMe?CalendarWidget(int.parse(_meCentury.toString()+_meYear.toString()),_meMonth,_meDay,(year,month,day,date,lunarDate,isLunar,hour){
            _meCentury = int.parse(year.toString().substring(0, 2));
            _meYear = int.parse(year.toString().substring(2, 4));
            _meMonth = month;
            _meDay = day;
            _meDate =date;
            _meLunraDate = lunarDate;
            _meIsLunar = isLunar;
            switch(hour){
              case 0:
                _meHour = 0;
                _meDay++;
                break;
              case 12:
                _meHour = 0;
                break;
              default :
                _meHour = hour;
            }

            setState(() {

            });
          },showHour: true,):
          CalendarWidget(int.parse(_otherCentury.toString()+_otherYear.toString()),_otherMonth,_otherDay,(year,month,day,date,lunarDate,isLunar,hour){
            _otherCentury = int.parse(year.toString().substring(0, 2));
            _otherYear = int.parse(year.toString().substring(2, 4));
            _otherMonth = month;
            _otherDay = day;
            _otherDate =date;
            _otherLunraDate = lunarDate;
            _otherIsLunar = isLunar;

            switch(hour){
              case 0:
                _otherHour = 0;
                _otherDay++;
                break;
              case 12:
                _otherHour = 0;
                break;
              default :
                _otherHour = hour;
            }
            setState(() {

            });
          },showHour: true,);
        });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
